package com.wang.luntan.users.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wang.luntan.common.pojo.ApiResponse;
import com.wang.luntan.common.pojo.ApiResponseEnum;
import com.wang.luntan.common.service.service.BaseServiceImpl;
import com.wang.luntan.common.util.ConstatFinalUtil;
import com.wang.luntan.common.util.EncryptUtil;
import com.wang.luntan.common.util.PageInfoUtil;
import com.wang.luntan.users.mapper.IAtAdminsDao;
import com.wang.luntan.users.mapper.IAtUsersDao;
import com.wang.luntan.users.pojo.AtAdmins;
import com.wang.luntan.users.pojo.AtUsers;
import com.wang.luntan.users.service.INoLoginOperService;
import com.wang.luntan.users.service.IUsersDbService;
import com.wang.luntan.users.service.IUsersOperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 登陆后的一些操作 一个Controller对应一个Service操作
 *
 * @param <T>
 * @author wangsh
 */
@Service("usersOperService")
public class UsersOperServiceImpl<T> extends BaseServiceImpl implements IUsersOperService {
    @Autowired
    private IUsersDbService usersDbService;
    @Autowired
    private INoLoginOperService noLoginOperService;
	@Autowired
	private IAtAdminsDao adminsDao;
    @Autowired
    private IAtUsersDao atUsersDao;
    @Override
    public ApiResponse<Object> logout(Map<String, Object> paramsMap) {
        return null;
    }

    @Override
    public ApiResponse<AtUsers> findOneAtUsersService(Map<String, Object> condMap) {
        ApiResponse<AtUsers> apiResponse = new ApiResponse<AtUsers>();
        apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
        Map<String, AtUsers> data = new HashMap<String, AtUsers>();
        AtUsers users = this.atUsersDao.findOne(condMap);
        /* 将结果放到JSON中 */
        data.put(ApiResponseEnum.NAME_ONE.getName(), users);
        apiResponse.setDataOne(data);
        apiResponse.setDataOneJava(users);
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }
    /**
     * 添加的用户list打开
     * */
    @Override
    public ApiResponse<AtUsers> findCondListusersService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
        ApiResponse<AtUsers> apiResponse = new ApiResponse<AtUsers>();
        apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
        List<AtUsers> usersList = Collections.EMPTY_LIST;

        Map<String, List<AtUsers>> dataList = new HashMap<String, List<AtUsers>>();
        /* 对关键字进行模糊匹配 */
        if (condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + "")) {
            condMap.put("keyword", "%" + condMap.get("keyword") + "%");
        }

        /* 设置分页相关信息 */
        if (pageInfoUtil != null) {
            /* 设置分页信息 */
            Page<AtAdmins> page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
            usersList = this.atUsersDao.findList(condMap);
            pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal() + ""));
            /* 向接口中放分页信息 */
            apiResponse.setPageInfoUtil(pageInfoUtil);
        } else {
            usersList = this.atUsersDao.findList(condMap);
        }

        /* 将结果放到JSON中 */
        dataList.put(ApiResponseEnum.NAME_LIST.getName(), usersList);
        apiResponse.setDataListJava(usersList);
        apiResponse.setDataList(dataList);
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }
    @Override
    public ApiResponse<AtUsers> usersUpdate(Map<String, Object> paramsMap) {
        /* 获取用户的Id */
        String id = paramsMap.get("id") + "";
        Map<String, Object> condMap = new HashMap<String, Object>();
        condMap.put("id", id);
        ApiResponse<AtUsers> apiResponse = this.usersDbService.findOneUsersService(condMap);
        /* 清空信息 */
        apiResponse.setInfo("", Collections.EMPTY_MAP);
        return apiResponse;
    }

    @Override
    public ApiResponse<Object> operUsersUpdateSubmit(Map<String, Object> paramsMap) {
        ApiResponse<Object> apiResponse = new ApiResponse<Object>();
        /* 获取用户的Id */
        String id = paramsMap.get("id") + "";

        String operType = paramsMap.get("operType") + "";

        boolean upflag = false;
        Map<String, Object> condMap = new HashMap<String, Object>();
        condMap.put("id", id);
        ApiResponse<AtUsers> apiResponseRes = this.usersDbService.findOneUsersService(condMap);
        AtUsers users = apiResponseRes.getDataOneJava();
        if ("updateInfo".equalsIgnoreCase(operType)) {
            String nickName = paramsMap.get("nickName") + "";
            String trueName = paramsMap.get("trueName") + "";
            String birthdayStr = paramsMap.get("birthdayStr") + "";
            String address = paramsMap.get("address") + "";
            String qq = paramsMap.get("qq") + "";
            String phone = paramsMap.get("phone") + "";
            String regionId = paramsMap.get("regionId") + "";
            String sex = paramsMap.get("sex") + "";
            String weiXin = paramsMap.get("weiXin") + "";
            String education = paramsMap.get("education") + "";

            /* 根据不同的请求跳转不同的页面 */
            users.setNickName(nickName);
            users.setTrueName(trueName);
            users.setBirthday(this.dateFormatUtil.strDateTime(birthdayStr));
            users.setAddress(address);
            users.setQq(qq);
            users.setPhone(phone);
            users.setWeiXin(weiXin);
            users.setSex(Byte.valueOf(sex));

            upflag = true;
        } else if ("updatePass".equalsIgnoreCase(operType)) {
            String password = paramsMap.get("password") + "";
            String souPass = paramsMap.get("souPass") + "";

            if (this.encryptUtil.checkStr(souPass, users.getPassword())) {
                users.setPassword(this.encryptUtil.encodeStr(password));
                users.setSouPass(password);
                upflag = true;
            } else {
                /* 原密码不正确 */
                apiResponse.setCode(ApiResponseEnum.INFO_SOUPASS_INCORRECT.getStatus());
            }
        } else if ("updateEmail".equalsIgnoreCase(operType)) {
            String email = paramsMap.get("email") + "";
            /* 更新邮箱 */
            users.setEmail(email);

            upflag = true;
            if (ApiResponseEnum.STATUS_SUCCESS.getStatus() == apiResponse.getCode()) {
                /* 修改成功,发送邮件 */
                if (ApiResponseEnum.STATUS_SUCCESS.getStatus() == apiResponse.getCode()) {
                    /* 注册成功,发送邮件 */
                    apiResponse = this.noLoginOperService.operEmailVerfySendService(paramsMap);
                }
            }
        } else if ("updatePhoto".equalsIgnoreCase(operType)) {
            users.setUpdateTime(new Date());
            /* 注册成功,发送邮件 */
            apiResponse = this.usersDbService.updateOneUsersService(users, paramsMap);
            /* 直接就更新了, */
        }

        if (upflag) {
            /* 更新用户 */
            users.setUpdateTime(new Date());
            apiResponse = this.usersDbService.updateOneUsersService(users);
        }
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }

    @Override
    public JSONObject loginAdminsService(JSONObject condJSON) {
        /* 返回结果的JSON */
        JSONObject resultJSON = new JSONObject();
        AtAdmins admins = new AtAdmins();
        EncryptUtil encryptUtil = new EncryptUtil();

        String password = condJSON.getString("password");
        String accountNumber = condJSON.getString("accountNumber");

        if (!"".equals(accountNumber) && accountNumber != null) {
            admins = (AtAdmins) adminsDao.login(condJSON);
        } else {
            admins = null;
        }

        if (admins != null) {
            if (encryptUtil.checkStr(password, admins.getPassword())) {
                if ("禁用".equals(admins.getStatusStr())) {
                    resultJSON.put("code", "1");
                    resultJSON.put("info", "该账户已被禁用");
                } else {
                    admins.setLoginCount(admins.getLoginCount() + 1);
                    adminsDao.update(admins);
                    /* 将管理员的json存储到json中 */
                    JSONObject dataJSON = admins.toJSON();
                    resultJSON.put("data", dataJSON);
                    resultJSON.put("code", "0");
                    resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
                    resultJSON.put("name", admins.getName());
                }

            } else {
                adminsDao.update(admins);

                resultJSON.put("code", "2");
                resultJSON.put("info", "密码错误");
                resultJSON.put("name", admins.getName());
            }
        } else {
            resultJSON.put("code", "2");
            resultJSON.put("info", "用户名错误");
        }
        return resultJSON;
    }

    @Override
    public ApiResponse<Object> verifyAdminsAuthSerivce(Map<String, Object> paramsMap) {
        ApiResponse<Object> apiResponse = new ApiResponse<Object>();
        /* 默认木有权限 */
        apiResponse.setCode(ApiResponseEnum.INFO_AUTH_NOT.getStatus());

        Map<String, Object> condMap = new HashMap<String, Object>();
        /* -------获取参数------ */
        String adminsId = paramsMap.get("adminsId") + "";
        String currentUrl = paramsMap.get("currentUrl") + "";
        /* ------根据管理员id查询管理员对象---- */
        condMap.clear();
        condMap.put("id", adminsId);
        ApiResponse<AtAdmins> adminsResponse = this.usersDbService.findOneAdminsService(condMap);
        AtAdmins admins = adminsResponse.getDataOneJava();

        /* 根据角色id查询角色菜单表 */
        condMap.clear();
        condMap.put("status", 1);
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }

    /*获取该角色所拥有的可用的菜单*/
    @Override
    public ApiResponse<Object> getRoleMenuSerivce(Map<String, Object> paramsMap) {
        ApiResponse<Object> apiResponse = new ApiResponse<Object>();
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }
}
